package com.szxy.lambda;

import com.szxy.bean.Employee;
import org.junit.jupiter.api.Test;

import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeSet;

/**
 * @Author:zwer
 * @Date:2020/4/14 16:40
 * @Description:com.szxy.lambda
 * @Version:1.0
 * Lambda 表达式
 * 作用：减少代码冗余，提高代码的可读性
 * java.lang.Runnable: public void run() {}
 * java.util.Comparator: public int compare(T o1, T o2) {}
 * 1. 只适用于函数式接口
 **/
public class LambdaTest {

    @Test
    public void test1() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("Hello World!!!");
            }
        }).start();
    }


    @Test
    public void test2() {
        new Thread( () -> System.out.println("Hello World!!!") ).start();
    }

    @Test
    public void test3() {
        // 1. 实现 java.util.Comparator 接口的 compare 方法
        // 2. Employee 继承 Comparable 接口
        TreeSet<Employee> ts = new TreeSet<>(new Comparator<Employee>() {
            @Override
            public int compare(Employee o1, Employee o2) {
                return  Double.compare(o1.getId(), o2.getId());
            }
        });
        ts.add(new Employee(1, "张三", 100));
        ts.add(new Employee(5, "李四", 100));
        ts.add(new Employee(3, "王五", 100));
        for (Employee t : ts) {
            System.out.print(t+" \n");
        }
        System.out.println();
    }


    @Test
    public void test4() {
        TreeSet<Employee> ts = new TreeSet<>((e1, e2) ->
             Double.compare(e1.getId(), e2.getId())
        );
        ts.add(new Employee(1, "张三", 100));
        ts.add(new Employee(5, "李四", 100));
        ts.add(new Employee(3, "王五", 100));
        for (Employee t : ts) {
            System.out.print(t+" \n");
        }
        System.out.println();
    }

    @Test
    public void test5() {
        TreeSet<Employee> ts = new TreeSet<>(Comparator.comparingDouble(Employee::getId));
        ts.add(new Employee(1, "张三", 100));
        ts.add(new Employee(5, "李四", 100));
        ts.add(new Employee(3, "王五", 100));
//        Iterator<Employee> iter = ts.iterator();
//        while (iter.hasNext()) {
//            System.out.println(iter.next());
//        }

        ts.removeIf(emp -> emp.getId() < 2);
        ts.forEach(System.out::println);
    }



}

